package application;

import application.dao.DBService;
import application.dao.entity.News;
import application.dao.entity.Notice;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuItem;
import javafx.scene.web.HTMLEditor;

import java.net.URL;
import java.util.ResourceBundle;

public class Manager_information_controller extends BaseController implements Initializable {


    @FXML
    public HTMLEditor htmlEditor;
    public javafx.stage.Stage stage;
    @FXML
    private Menu menu_join;

    @FXML
    private MenuItem menu_join_competitons;

    @FXML
    private Menu menu_main;

    @FXML
    private MenuItem menu_join_users;

    @FXML
    private Menu menu_person;

    @FXML
    private Menu menu_informations;

    @FXML
    private Button putOnAnnouncement;

    @FXML
    private Menu menu_scores;

    @FXML
    private Button putOnNews;

    @FXML
    void onClickAnnouncement(ActionEvent event) {
        String htmlText = htmlEditor.getHtmlText();
        String title = parseHtml(htmlText, 15);
        Notice notice = new Notice();
        notice.setAnmTitle(title);
        notice.setAnmText(htmlText);
        boolean b = DBService.addNotice(notice);
        BaseUtil.gotoMain(b, "发布通知失败", stage);
    }

    @FXML
    void onClickNews(ActionEvent event) {
        String htmlText = htmlEditor.getHtmlText();
        String title = parseHtml(htmlText, 15);
        News news = new News();
        news.setNewsTitle(title);
        news.setNewsText(htmlText);
        boolean b = DBService.addNews(news);
        BaseUtil.gotoMain(b, "发布新闻失败", stage);
    }


    @FXML
    void onCheckMenu_main(ActionEvent event) {

    }

    @FXML
    void onCheckMenu_person(ActionEvent event) {

    }

    @FXML
    void onCheckMenu_join(ActionEvent event) {

    }

    @FXML
    void onCheckMenu_join_users(ActionEvent event) {

    }

    @FXML
    void onCheckMenu_join_competitons(ActionEvent event) {

    }

    @FXML
    void onCheckMenu_scores(ActionEvent event) {

    }

    @FXML
    void onCheckMenu_informations(ActionEvent event) {

    }


    public String parseHtml(String html, int length) {

        if (html == null || html == "") {
            return html = "空";
        } else {
            if (html.length() < length) {
                return html;
            } else {
                html = html.replaceAll("<.*?>", " ").replaceAll("", "");
                html = html.replaceAll("<.*?", "");
                return (html.substring(0, length) + "...");
            }
        }
    }

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
    }


    @Override
    public javafx.stage.Stage setStage(javafx.stage.Stage stage) {
        return this.stage = stage;
    }

    @Override
    public void init() {

    }
}
